<?php
/*
 * 线索列表 
 * @Author: wjf
 * @Date: 2022-12-07 11:08:20
 * @LastEditTime: 2022-12-08 16:35:05
 * @LastEditors:  
 */

namespace app\crm\controller\clue;

use app\crm\model\client\Ourclients;
use app\crm\model\clue\Cluelist as ClueCluelist;
use app\crm\model\clue\Follow;
use app\crm\validate\clue\Cluelist as ValidateClueCluelist;
use think\Controller;
use think\facade\Session;
use think\Db;

class Cluelist extends Controller
{
    /**
     * 主页面
     * @Author: wjf
     * @Date: 2022-12-01 09:16:16
     */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 添加页面
     * @Author: wjf
     * @Date: 2022-11-26 15:53:22
     */
    public function add()
    {
        return $this->fetch();
    }
    /**
     * 添加插入数据库
     * @Author: wjf
     * @Date: 2022-11-26 15:53:50
     */
    public function data()
    {
        //1、验证请求方式
        if ($this->request->isPost() && $this->request->isAjax()) {
            //2、获取数据
            $data = $this->request->post();
            dump($data);
            //3、验证数据
            $validata = new ValidateClueCluelist();
            if ($validata->check($data) == false) {
                $this->error($validata->getError());
            }
            //、数据库操作
            $OuModel = new ClueCluelist();
            if ($OuModel->save($data)) {
                return json(['code' => 1, 'msg' => '添加成功']);
            } else {
                return json(['code' => 0, 'msg' => '添加失败']);
            }
        }
    }

    /**
     * 数据，分页
     * @Author: wjf
     * @Date: 2022-11-19 17:19:17
     */
    public function list()
    {
        if ($this->request->param('data')) {
            $aa = $this->request->get('data');
            $arr = [];
            foreach ($aa as $k => $v) {
                if (!$v) {
                    unset($aa[$k]);
                } else {
                    $arr[$k] = $v;
                }
            }
            $page = ($_GET['page'] - 1) * $_GET['limit'];
            $data = ClueCluelist::where($arr)->where('status', 3)->select();
            $count = ClueCluelist::where('status', 3)->count();
            return json(['code' => 0, 'msg' => '遍历', 'count' => $count, 'data' => $data]);
        } else {
            $page = ($_GET['page'] - 1) * $_GET['limit'];
            $data = ClueCluelist::limit($page, $_GET['limit'])->where('status', 3)->where('super_id', 1)->where('classify', 2)->select();
            $count = ClueCluelist::where('status', 3)->where('super_id', 1)->where('classify', 2)->count();
            return json(['code' => 0, 'msg' => '遍历', 'count' => $count, 'data' => $data]);
        }
    }

    /**
     * 遍历数据
     * @Author: wjf
     * @Date: 2022-11-26 18:28:36
     */
    public function edit()
    {
        if (!empty($_GET['id'])) {
            Session::set('e_id', $_GET['id']);
            $list = ClueCluelist::where('id', $_GET['id'])->select();
            $this->assign(['list' => $list]);
        }
        return $this->fetch();
    }

    /**
     * 跟新
     * @Author: wjf
     * @Date: 2022-11-27 21:58:02
     */
    public function amend()
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            $data = $this->request->post();
            $ModelClientOurclients = new ClueCluelist;
            if ($ModelClientOurclients->allowField(true)->isUpdate(true)->save($data, ['id' => Session::get('e_id')])) {
                return json(['code' => 1, 'msg' => '跟新成功']);
            } else {
                return json(['code' => 0, 'msg' => '跟新失败']);
            }
        }
    }

    /**
     * 放入线索池
     * @Author: wjf
     * @Date: 2022-12-04 18:12:17
     */
    public function putin()
    {
        // 启动事务
        Db::startTrans();
        if ($this->request->isPost() && $this->request->isAjax()) {
            $id = $_POST['id'];
            $arr = explode(',', $id);
            array_pop($arr);
            try {
                foreach ($arr as $k => $v) {
                    Ourclients::where('id', $v)->update(['super_id' => 0, 'status' => 3, 'classify' => 2]);       
                }
                // 提交事务
                Db::commit();
                return json(['code' => 0, 'msg' => '放入成功']);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(['code' => 1, 'msg' => '放入失败']);
            }
        }
    }

    //转为客户
    public function highseas()
    {
        // 启动事务
        Db::startTrans();
        if ($this->request->isPost() && $this->request->isAjax()) {
            $id = $_POST['id'];
            try {
                Ourclients::where('id',$id)->update(['super_id' => 1, 'status' => 2, 'classify' => 1]);
                Follow::where('classify_id',$id)->update(['classify' => 1]);
                // 提交事务
                Db::commit();
                return json(['code' => 0, 'msg' => '转客成功']);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(['code' => 1, 'msg' => '转客失败']);
            }
        }
    }
}
